//
//  MyScrollView.m
//  ScrollView应用
//
//  Created by qianfeng on 15/12/30.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "CTJScrollView.h"
#import "ParseUrlManager.h"
#import "UIKit+AFNetworking.h"
#define RGB(A, B, C, Alpha)    [UIColor colorWithRed:A/255.0 green:B/255.0 blue:C/255.0 alpha:Alpha]

@interface CTJScrollView()

@property (nonatomic,strong) NSMutableArray * imageArr;

@property (nonatomic,strong) NSMutableArray * topicArr;

@property (nonatomic,strong) UIScrollView * scrollView;

@property (nonatomic,strong) UIPageControl * pageCon;

@property (nonatomic,assign) NSInteger pageCount;

@end

@implementation CTJScrollView
{
    NSArray * titleArr;
    NSInteger index;
    NSTimer * timer;
}

-(id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self ParseData];
    }
    return self;
}

-(void)ParseData{
    
    [ParseUrlManager ParseDataWithUrl:@"http://api.liwushuo.com/v2/banners?channel=iOS" success:^(id data) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        for (NSMutableDictionary * mdic in dic[@"data"][@"banners"]) {
            
            NSString * url = mdic[@"image_url"];
            [self.topicArr addObject:url];
        }
        
        [self initParameter];
     } fail:^(NSError *error) {}];
}

-(void)initParameter{
    
    index = 0;
    self.pageCount = self.topicArr.count;
    [self createScrollView];
    timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(pushNextPic) userInfo:nil repeats:YES];
    
    
    for (int i = 0;i < self.topicArr.count;i++) {
        
        [ParseUrlManager ParseDataWithUrl:self.topicArr[i] success:^(id data) {
            UIImage * image = [UIImage imageWithData:data];
            [self.imageArr replaceObjectAtIndex:i withObject:image];
            
        } fail:^(NSError *error) {}];
    }

}

-(NSMutableArray *)imageArr{
    if (!_imageArr) {
        _imageArr = [NSMutableArray array];
        for (int i = 0; i < self.pageCount; i++) {
            UIImage * image = [[UIImage alloc] init];
            [_imageArr addObject:image];
        }
    }
    return _imageArr;
}

-(NSMutableArray *)topicArr{
    if (!_topicArr) {
        
        _topicArr = [[NSMutableArray alloc] init];
    }
    return _topicArr;
}

-(void)createScrollView{
    //创建滚动视图
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.scrollView.backgroundColor = [UIColor colorWithRed:218.0/255 green:218.0/255 blue:218.0/255 alpha:1];
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*3, self.scrollView.frame.size.height);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    for (int i = 0; i <= 2; i++) {
        UIImage * imageName = nil;
        if (i == 0)
            imageName = self.imageArr.lastObject;
        else
            imageName = self.imageArr[i-1];
        
        UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width *i, 0, self.frame.size.width, self.frame.size.height)];
        imageV.contentMode = UIViewContentModeScaleAspectFit;
        imageV.image = imageName;
        imageV.tag = 100+i;
        [self.scrollView addSubview:imageV];
    }
    
    [self addSubview:self.scrollView];

    [self createPageCon];
}
//创建pagecontroller
-(void)createPageCon{
    
    self.pageCon = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height/6*5, self.frame.size.width, self.frame.size.height/6)];
    self.pageCon.numberOfPages = self.pageCount;
    self.pageCon.currentPage = index;
    self.pageCon.userInteractionEnabled = NO;
    self.pageCon.backgroundColor = [UIColor clearColor];
    self.pageCon.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.pageCon.pageIndicatorTintColor = RGB(255, 255, 255,0.1);
    
    [self addSubview:self.pageCon];
}
/**
    重置图片
 */
-(void)resetImage{
    
    int a = 0;
    for (NSInteger i = index-1; i <= index+1; i++) {
        UIImage * imageName = nil;
        if (i == -1) {
            imageName = self.imageArr[self.imageArr.count-1];
        }else{
            imageName = self.imageArr[i % self.imageArr.count];
        }
        UIImageView * imageV = (id)[self viewWithTag:100+a++];
        imageV.image = imageName;
    }
}

/**
    协议方法
 */
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [timer setFireDate:[NSDate distantFuture]];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
   
    [self PicChange];
    [timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:3]];
}

/**
    推进下一页动画
 */
-(void)pushNextPic{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width*2, 0);
    } completion:^(BOOL finished) {
        [self PicChange];
    }];
}

-(void)PicChange{
    
    if (self.scrollView.contentOffset.x > self.frame.size.width)
        index++;
    else
        if (self.scrollView.contentOffset.x < self.frame.size.width)
            index--;
    if (index == self.imageArr.count)
        index = 0;
    
    if (index == -1)
        index = self.imageArr.count-1;
    
    self.pageCon.currentPage = index;
    self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    [self resetImage];
}

@end
